package com.hyacinth.server.constant;

import org.fusesource.jansi.Ansi;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Random;

/**
 * Function:
 *
 * @author zhangfb
 *         Date: 2018/9/10 14:07
 * @since JDK 1.8
 */
public final class Constant {

    public static final Ansi.Color[] colors = Ansi.Color.values();

    public final static String SERVER_PORT = "server.port";
    public final static String SERVER_CONTEXT_PATH = "server.context.path";
    public final static String SERVER_CHANNEL = "server.channel";

    public static final class ContentType {
        public final static String JSON = "application/json; charset=UTF-8";
        public final static String TEXT = "text/plain; charset=UTF-8";
        public final static String HTML = "text/html; charset=UTF-8";
        public final static String SET_COOKIE = "Set-Cookie";
        public final static String COOKIE = "Cookie";
    }

    public static final class SystemProperties {
        public static final String LOGO = genLogo();

        public final static String APPLICATION_PROPERTIES = "aurora.properties";
        public final static String APPLICATION_THREAD_MAIN_NAME = "☝( ◠‿◠ )☝";
        public final static String APPLICATION_THREAD_BOOS_NAME = "boos";
        public final static String APPLICATION_THREAD_WORK_NAME = "(♛‿♛)";
        public final static String APPLICATION_THREAD_SHUTDOWN_NAME = "(〒︿〒)";
    }


    private static String genLogo() {
        String LOGO = "\n" +
                "\t\t\t               (       )  (             \n" +
                "\t\t\t   (           )\\ ) ( /(  )\\ )   (      \n" +
                "\t\t\t   )\\       ( (()/( )\\())(()/(   )\\     \n" +
                "\t\t\t((((_)(     )\\ /(_)|(_)\\  /(_)|(((_)(   \n" +
                "\t\t\t )\\ _ )\\ _ ((_|_))   ((_)(_))  )\\ _ )\\  \n" +
                "\t\t\t (_)_\\(_) | | | _ \\ / _ \\| _ \\ (_)_\\(_) \n" +
                "\t\t\t  / _ \\ | |_| |   /| (_) |   /  / _ \\   \n" +
                "\t\t\t /_/ \\_\\ \\___/|_|_\\ \\___/|_|_\\ /_/ \\_\\ ";

        BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(LOGO.getBytes(Charset.forName("utf8"))), Charset.forName("utf8")));

        String line;
        StringBuilder sb = new StringBuilder();
        try {
            while ( (line = br.readLine()) != null ) {
                if(!line.trim().equals("")){
                    line = decorate(line);
                    sb.append(line).append("\r\n");
                }
            }
        } catch (IOException e) {
            return LOGO;
        }
        return sb.toString();
    }

    public static String decorate(String line) {
        Ansi.Color color = colors[genRandom()];
        return String.valueOf(Ansi.ansi().eraseScreen().fg(color).a(line).reset());
    }

    public static int genRandom() {
        Random r = new Random();
        return r.nextInt(9);
    }
}
